package fun.anin.aninhongbao.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 结果
 * @author anin
 * @param <T> 泛型
 * @date: 2019年1月22日下午4:20:31
 */
@Data
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;
    private String message;
    private T data;

    /**
     * ok
     * @return  R
     */
    public static R ok() {
        return other(RE.OK);
    }

    /**
     * ok
     * @param msg   msg
     * @return      R
     */
    public static R ok(String msg) {
        return other(RE.OK.getCode(),msg);
    }

    /**
     * R
     * @param data  data
     * @return      R
     */
    public static R ok(Object data) {
        R r = other(RE.OK);
        r.setData(data);
        return r;
    }

    /**
     * other
     * @param re    re
     * @return      R
     */
    public static R other(RE re) {
        R r = new R();
        r.setCode(re.getCode());
        r.setMessage(re.getMsg());
        return r;
    }

    /**
     * error
     * @return  R
     */
    public static R error() {
        return other(RE.ERROR);
    }

    /**
     * error
     * @param msg   msg
     * @return      R
     */
    public static R error(String msg) {
        return other(RE.ERROR.getCode(), msg);
    }

    /**
     * other
     * @param code  code
     * @param msg   msg
     * @return      R
     */
    public static R other(int code, String msg) {
        R r = new R();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }

    /**
     * 私有化构造
     */
    private R() {
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
